package zabbix

import (
	"encoding/json"
	"errors"
)

func DashboardCreateData() (string,map[string]interface{}) {
	method := "dashboard.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "My dashboard",
        "widgets": [
            {
                "type": "problems",
                "x": 0,
                "y": 0,
                "width": 6,
                "height": 5,
                "fields": [
                    {
                        "type": 1,
                        "name": "tags.tag.0",
                        "value": "service"
                    },
                    {
                        "type": 1,
                        "name": "tags.value.0",
                        "value": "zabbix_server"
                    }
                ]
            }
        ],
        "userGroups": [
            {
                "usrgrpid": "7",
                "permission": "2"
            }
        ],
        "users": [
            {
                "userid": "4",
                "permission": "3"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func DashboardDeleteData() (string,map[string]interface{}) {
	method := "dashboard.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "2",
        "3"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func DashboardUpdateData() (string,map[string]interface{}) {
	method := "dashboard.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "dashboardid": "2",
        "userid": "1"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func DashboardGetData() (string,map[string]interface{}) {
	method := "dashboard.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectWidgets": "extend",
        "selectUsers": "extend",
        "selectUserGroups": "extend",
        "dashboardids": [
            "1",
            "2"
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func DashboardCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DashboardCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func DashboardDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DashboardDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func DashboardUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DashboardUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func DashboardGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DashboardGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

